/**
 * Created by gongxun on 16/11/20.
 */

var RecordDetailedHelper = function(){
    var that = {};

    that.saveKey = "recordDetailedKeys";

    //查询是否存在
    that.getIsRecordDetailed = function(ReplayId){
        var key = that.getRecordDetailedKey(ReplayId);
        return mo.fileHelper.getItem(key) != undefined;
    };

    //插入
    that.insertRecordDetailed = function(ReplayId, playInfoDetail){
        var key = that.getRecordDetailedKey(ReplayId);
        var value = JSON.stringify(playInfoDetail);
        mo.fileHelper.setItem(key, value);

        that.saveRecordDetailedKey(key);
    };

    //读取
    that.getRecordDetailed = function(ReplayId){
        var key = that.getRecordDetailedKey(ReplayId);
        var value = mo.fileHelper.getItem(key);
        return JSON.parse(value);
    };

    //读取for key
    that.getRecordDetailedForKey = function(key){
        var value = mo.fileHelper.getItem(key);
        if (value){
            return JSON.parse(value);
        }else{
            return null;
        }
    };

    //通过3个id生成key
    that.getRecordDetailedKey = function(ReplayId){
        var key = "uid_" + mo.gameUserInfo.uid + "_replayid_" + ReplayId;
        return key;
    };

    //通过ReplayImage生成RePlaykwx
    that.getRePlaykwxForRelayImage = function(replayImage, gameId){
        var size = replayImage.limit - replayImage.offset;
        var arrData = new Uint8Array(size);
        for (var i = 0; i < size; i++){
            arrData[i] = replayImage.view[i + replayImage.offset];
        }

        //gzip解压
        var replayData = pako.ungzip(arrData);

        //生成protobuf
        var msg = null;
        try {
            if (gameId == 14){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaykwx");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 15){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaykwxpp");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 9){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaynn");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 21){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlayttz");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 22){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlayzjh");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 20){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaywhmj");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 23){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaydzpk");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 25){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaydtj");
                msg = messageBuilder.decode(replayData);
            }else if (gameId == 26){
                var messageBuilder = mo.gameMsgManager.builder.build("gt_msg.RePlaypdk");
                msg = messageBuilder.decode(replayData);
            }
        } catch (e) {
            if (e.decoded) {
                that.msg = e.decoded;
            } else {
                that.msg = null;
                cc.log("解析protobuf失败");
            }
        }
        return msg;
    };

    //保存已经下载的录像
    that.saveRecordDetailedKey = function(key){
        var value = [];

        var valueStr = mo.fileHelper.getItem(that.saveKey);
        if (valueStr){
            value = JSON.parse(valueStr);
        }

        value.push(key);
        var saveValue = JSON.stringify(value);
        mo.fileHelper.setItem(that.saveKey, saveValue);
    };

    //读取已经下载的录像
    that.getRecordDetailedKeys = function(){
        var valueStr = mo.fileHelper.getItem(that.saveKey);

        var value = [];
        if (valueStr){
            value = JSON.parse(valueStr);
        }

        //过滤掉不是自己id保存的
        for (var i = 0; i < value.length; i++){
            var findnum = value[i].indexOf(mo.gameUserInfo.uid, 4);
            if (findnum == -1){
                value.splice(i, 1);
            }
        }

        return value;
    };

    //播放录像
    that.playReplayForId = function(replayId){
        var rePlayResponse = mo.recordDetailedHelper.getRecordDetailed(replayId);
        var gameId = rePlayResponse.GameId;
        var rePlaykwx = mo.recordDetailedHelper.getRePlaykwxForRelayImage(rePlayResponse.Replay2Gzip, gameId);

        if (gameId == 14 || gameId == 15){
            if (gameId == 14){
                mo.gameMsgManager.kwxGameModel = 1;
            }else if (gameId == 15){
                mo.gameMsgManager.kwxGameModel = 2;
                rePlaykwx = mo.gameHelper.replaceJsonTokwx(rePlaykwx, {});
            }

            mo.sceneHelper.runSceneKwx();
            mo.kwxTable.loadReplay(rePlaykwx, replayId);
        }else if (gameId == 9){
            mo.sceneHelper.runSceneNn();
            mo.nnTable.loadReplay(rePlaykwx, replayId);
        }else if (gameId == 21){
            mo.sceneHelper.runSceneGsbdx();
            mo.gsbdxTable.loadReplay(rePlaykwx, replayId);
        }else if (gameId == 22){
            mo.sceneHelper.runScenePsz();
            mo.pszTable.loadReplay(rePlaykwx, replayId);
        }else if(gameId == 20){
            mo.sceneHelper.runSceneWhmj();
            mo.whmjTabel.loadReplay(rePlaykwx, replayId);
        }else if(gameId == 23){
            mo.sceneHelper.runSceneDzpk();
            mo.dzpkTable.loadReplay(rePlaykwx, replayId);
        }else if(gameId == 25){
            mo.sceneHelper.runSceneDtj();
            mo.dtjTable.loadReplay(rePlaykwx, replayId);
        }else if (gameId == 26){
            mo.sceneHelper.runScenePdk();
            mo.pdkTable.loadReplay(rePlaykwx, replayId);
        }
    };

    //返回索引结构
    that.getSortRecordArray = function(recordInfos){
        //按照时间降序排列
        recordInfos.sort(function(a, b){
            return b.BeginTime - a.BeginTime;
        });

        var sortRecordArray = [];

        var sortRecord = null;
        for (var i = 0; i < recordInfos.length; i++){
            var recordInfo = recordInfos[i];
            if (sortRecord == null){
                sortRecord = that.getSortRecordItem(recordInfo);
                sortRecordArray.push(sortRecord);
            }else{
                if(sortRecord.JuId == recordInfo.JuId){

                }else{
                    sortRecord = that.getSortRecordItem(recordInfo);
                    sortRecordArray.push(sortRecord);
                }
            }
            sortRecord.items.push(recordInfo);
        }

        for(var i = 0; i < sortRecordArray.length; i++){
            var sortRecord = sortRecordArray[i];
            for (var j = 0; j < sortRecord.items.length; j++){
                var recordInfo = sortRecord.items[j];

                for (var m = 0; m < recordInfo.Uid.length; m++){
                    if (recordInfo.Uid[m] == mo.gameUserInfo.uid){
                        sortRecord.allScore += recordInfo.Score[m];
                    }
                }

                if (j == sortRecord.items.length - 1){
                    sortRecord.startTime = recordInfo.BeginTime;
                }
            }
        }

        return sortRecordArray;
    };

    //返回战绩
    that.getSortRecordItem = function(recordInfo){
        var sortRecord = {};
        sortRecord.items = [];
        sortRecord.allScore = 0;
        sortRecord.startTime = 0;
        sortRecord.JuId = recordInfo.JuId;
        sortRecord.TableName = "局id:" + recordInfo.JuId;

        return sortRecord;
    };

    return that;
};

mo.recordDetailedHelper = new RecordDetailedHelper();